package com.ruoyi.framework.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;


/**
 * MybatisPlus自动填充
 */
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        String userBy = null;
        try {
            LoginUser loginUser = SecurityUtils.getLoginUser();
            userBy = loginUser.getUsername();
        } catch (Exception e) {
        }
        this.setFieldValByName("createTime", DateUtils.getNowDate(), metaObject);
        this.setFieldValByName("createBy", userBy, metaObject);
        this.setFieldValByName("updateTime", DateUtils.getNowDate(), metaObject);
        this.setFieldValByName("updateBy", userBy, metaObject);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        String userBy = null;
        try {
            LoginUser loginUser = SecurityUtils.getLoginUser();
            userBy = loginUser.getUsername();
        } catch (Exception e) {
        }
        this.setFieldValByName("updateTime", DateUtils.getNowDate(), metaObject);
        this.setFieldValByName("updateBy", userBy, metaObject);
    }
}
